package com.easyfun.healthmagicbox;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.easyfun.healthmagicbox.base.DatabaseAccess;
import com.easyfun.healthmagicbox.base.DatabaseAccessImplementation;
import com.easyfun.healthmagicbox.comm.HealthThreadPool;
import com.easyfun.healthmagicbox.comm.SharePersonTask;
import com.easyfun.healthmagicbox.comm.UserToPersonsDownloadTask;
import com.easyfun.healthmagicbox.gallary.GridViewActivity;
import com.easyfun.healthmagicbox.ormlite.DatabaseHelper;
import com.easyfun.healthmagicbox.ormlite.ServerResponseCode;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.pojo.PersonProfile;
import com.easyfun.healthmagicbox.pojo.UserToPersons;
import com.easyfun.healthmagicbox.sync.BasicCommCallback;
import com.easyfun.healthmagicbox.util.PersonProfileCache;
import com.easyfun.healthmagicbox.util.ViewAdapter;
import com.easyfun.healthmagicbox.widget.CustomViewPager;
import com.easyfun.healthmagicbox.widget.DialogBuilder;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.zxing.decoding.Intents.Share;

import android.app.Activity;
import android.app.ActivityGroup;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;

@SuppressWarnings("deprecation")
public class ViewPagerActivity extends ActivityGroup implements DatabaseAccess{
	//
	private int m_circleid = 0;
	
	private String m_personid;
	// 
	private static CustomViewPager pager = null;
	// 
	public static CustomViewPager getPager() {
		return pager;
	}

	private ArrayList<View> views;
	//
	private ViewAdapter adapter;
	// 
	private LinearLayout circle_layout;
	// 
	private ImageView[] circle_imageViews;

	
	/**
	 * create all views
	 */
	private void setupView() {
		//addbackButton should after setting content view
		addbackButton(this);
		//
		createViews();
		// 
		circle_imageViews = new ImageView[views.size()];
		// 
		addCircle();
		pager = (CustomViewPager) findViewById(R.id.viewPager);
		// 
		adapter = new ViewAdapter(views);
		pager.setAdapter(adapter);
		// 
		setListener();
	}
	
	
	@Override
	public DatabaseHelper getHelper(Context context) {
		return DatabaseAccessImplementation.getInstance().getHelper(context);
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.viewpager);
		
		
		ImageButton sharebutton = (ImageButton) findViewById(R.id.title_share_btn);
		OnClickListener clicklistener = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				final Intent intent = new Intent(ViewPagerActivity.this, SharePageActivity.class);
	        	intent.setClass(ViewPagerActivity.this, SharePageActivity.class);
	        	intent.putExtra(ConstantData.HMBPERSONID, m_personid);
				startActivity(intent);//
			}
		};
		sharebutton.setOnClickListener(clicklistener );
		
		RelativeLayout rightpress = (RelativeLayout) findViewById(R.id.rightpress);
		rightpress.setOnClickListener(clicklistener);
		// 
		m_personid = getIntent().getStringExtra(ConstantData.HMBPERSONID);
		//
		if ((m_personid == null) || (m_personid.length() == 0))
		{
			setResult(RESULT_CANCELED);
			finish();
			return;
		}
		// new user has not person profile record so jump to another page to input
		try {
			if (!m_personid.equalsIgnoreCase(ConstantData.ADD_PERSON))				
			{
				Dao<PersonProfile, Integer> profileDao = getHelper(this).
						getPersonProfileDao();
				List<PersonProfile> pps = profileDao.queryForEq("personID", m_personid);
				
				//no profile or first input after registered.
				if ((pps.size()==0)||(pps.get(0).getManualinput()==ConstantData.ManualInputNo))
				{
					final Intent intent = new Intent(this, ProfileActivity.class);
		        	intent.setClass(ViewPagerActivity.this, ProfileActivity.class);
		        	intent.putExtra(ConstantData.HMBPERSONID, m_personid);
					startActivity(intent);//
					setResult(RESULT_CANCELED);
					finish();
					return;
				}else
				{
					PersonProfile pp = null;
					//if already exists, get it.
					if (pps.size() == 1)
						pp = pps.get(0);
					/*
					if ((pp.getName().length()==0)&&(pp.getTitle().length()==0))
					{
						//it an invited people
						final Intent intent = new Intent(this, SharePageActivity.class);
			        	intent.setClass(ViewPagerActivity.this, SharePageActivity.class);
			        	intent.putExtra(ConstantData.HMBPERSONID, m_personid);
						startActivity(intent);//
						setResult(RESULT_CANCELED);
						finish();
						return;
					}else
					{
						//show this person info
					}
					*/
					
				}
			}else
			{
				String createType = getIntent().getStringExtra("createType");
				
				if (createType.equals("share"))
				{
										
				}else
				{
					final Intent intent = new Intent(this, ProfileActivity.class);
		        	intent.setClass(ViewPagerActivity.this, ProfileActivity.class);
		        	intent.putExtra(ConstantData.HMBPERSONID, m_personid);
					startActivity(intent);//
				}
				setResult(RESULT_CANCELED);
				finish();
				return;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			setResult(RESULT_CANCELED);
			finish();
			return;
		}
		
		setupView();
		// 
		select();

	}

	/**
	 * 
	 */
	private void addCircle() {
		circle_layout = (LinearLayout) findViewById(R.id.circle_holder);
		for (int i = 0; i < views.size(); i++) {
			ImageView imageView = new ImageView(this);
			imageView.setLayoutParams(new LayoutParams(15, 15));
			imageView.setPadding(5, 0, 5, 0);
			circle_imageViews[i] = imageView;
			if (i == m_circleid) {
				circle_imageViews[i].setBackgroundResource(R.drawable.guide_dot_white);
			} else {
				circle_imageViews[i].setBackgroundResource(R.drawable.guide_dot_black);
			}
			circle_layout.addView(imageView);
		}
	}

	/**
	 *
	 */
	private void createViews() {
		views = new ArrayList<View>();
		
		views.add(getLocalActivityManager().startActivity("One",
				new Intent(this, SubLastProfileActivity.class).putExtra(ConstantData.HMBPERSONID, m_personid)
				).getDecorView());
		views.add(getLocalActivityManager().startActivity("Two",
				new Intent(this, SubGraphicReportActivity.class).putExtra(ConstantData.HMBPERSONID, m_personid)
				).getDecorView());
		views.add(getLocalActivityManager().startActivity("Three",
				new Intent(this, SubDocoterSuggestionActivity.class).putExtra(ConstantData.HMBPERSONID, m_personid)
				).getDecorView());
		views.add(getLocalActivityManager().startActivity("Four",
				new Intent(this, GridViewActivity.class).putExtra(ConstantData.HMBPERSONID, m_personid)
				).getDecorView());
//		views.add(getLocalActivityManager().startActivity("Four",
//				new Intent(this, SubMedicalUsageActivity.class).putExtra(ConstantData.HMBPERSONID, m_personid)
//				).getDecorView());
		/* */

	}// createViews()---end

	/**
	 * 
	 */
	private void select() {
		for (int i = 0; i < views.size(); i++) {
			if (i == m_circleid) {
				pager.setCurrentItem(i);
			}
		}
	}

	/**
	 * pagerҳ
	 */
	private void setListener() {
		pager.setOnPageChangeListener(new OnPageChangeListener() {
			// 
			public void onPageSelected(int arg0) {
				// 
				for (int i = 0; i < circle_imageViews.length; i++) {
					circle_imageViews[arg0]
							.setBackgroundResource(R.drawable.guide_dot_white);
					if (arg0 != i) {
						circle_imageViews[i]
								.setBackgroundResource(R.drawable.guide_dot_black);
					}
				}
			}

			// 
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			// 
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}
	
	//because this class can't inherit from baseActivity so copy following
	
	protected void addbackButton(final Activity activity)
	{
		/*
		TextView txtGohome = (TextView) findViewById(R.id.txt_gohome);
		if (txtGohome!=null)
		{
			txtGohome.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {

					final Intent intenthome = new Intent(activity, HMBMainActivity.class);
		    	
					activity.startActivity(intenthome);//
				}
			});
		}
		*/
		RelativeLayout Goback = (RelativeLayout) findViewById(R.id.leftpress);
		if (Goback != null)
			Goback.setOnClickListener(new OnClickListener() {
			
				@Override
				public void onClick(View v) {
					activity.finish();
				
				}
			});
		ImageButton txtGoback = (ImageButton) findViewById(R.id.title_btn); //txt_goback);
		if (txtGoback!=null)
		{
			txtGoback.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					activity.finish(); 
					
				}
			});
		}
	}
	
	
	
	
}
